package com.wt.springsamples.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wt.springsamples.common.ApiResult;
import com.wt.springsamples.common.BizException;
import com.wt.springsamples.model.SysUser;
import com.wt.springsamples.model.dto.UserRoleConfigDto;
import com.wt.springsamples.model.search.UserSearchModel;
import com.wt.springsamples.biz.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("用户管理")
@RestController
@RequestMapping("/api/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/{id}")
    @ApiOperation(value = "user:view",nickname = "用户管理-查看")
//    @PreAuthorize("hasAuthority('user:view')")
    public ApiResult getUserInfo(@PathVariable("id") Long id) {
        if(id==null || id<0){
            throw  new BizException(5001,"业务异常");
        }
        int i = 1/0;
        SysUser user = sysUserService.getById(id);
        return ApiResult.ok(user);
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "user:deleteById",nickname = "用户管理-删除",extensions = {@Extension(name="",properties =@ExtensionProperty(name ="", value = ""))})
//    @PreAuthorize("hasAuthority('user:delete')")
    public ApiResult deleteUser(Long id) {
        System.out.println("user:delete");
        sysUserService.removeById(id);
        return ApiResult.ok("user:delete");
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "user:deleteBatch",nickname = "用户管理-批量删除")
//    @PreAuthorize("hasAuthority('user:delete')")
    public ApiResult deleteByIds(@RequestBody List<Integer> ids) {
        System.out.println("user:delete");
        sysUserService.removeByIds(ids);
        return ApiResult.ok("user:delete");
    }

    @PostMapping("/create")
//    @PreAuthorize("hasAuthority('user:create')")
    public ApiResult create(SysUser sysUser) {
        System.out.println("user:create");
        return ApiResult.ok("user:create");
    }

    @RequestMapping("/update")
//    @PreAuthorize("hasAuthority('user:update')")
    public ApiResult update(SysUser sysUser) {
        System.out.println("user:update");
        return ApiResult.ok("user:update");
    }

    @RequestMapping("/queryList")
    @ApiOperation(value = "user:query",nickname = "用户管理-查询")
//    @PreAuthorize("hasAuthority('user:query')")
    public ApiResult queryList(@RequestParam(required = false,defaultValue ="1") Integer pageNum
            , @RequestParam(required = false,defaultValue ="5") Integer pageSize
            , UserSearchModel searchModel){
        Page<SysUser> page = sysUserService.pagination(searchModel,pageSize, pageNum);
        return  ApiResult.ok(page);
    }

    @RequestMapping("/configRoles")
    @ApiOperation(value = "user:configRoles",nickname = "用户管理-角色配置")
//    @PreAuthorize("hasAuthority('user:role:config')")
    public ApiResult configRoles(@RequestBody UserRoleConfigDto userRoleConfigDto){
        sysUserService.configRole(userRoleConfigDto);
        return  ApiResult.ok();
    }

    @RequestMapping("/getRoleIds")
    @ApiOperation(value = "user:getConfiguredRoles",nickname = "用户管理-获取已配置角色列表")
    public ApiResult getConfiguredRoles(Long userId){
        List<Long> roleIdList = sysUserService.getRoleIdsByUserId(userId);
        return  ApiResult.ok(roleIdList);
    }

}
